Page({

  data: {
    building_distance:[],
    unit_distance:[],
    door_distance:[],
    voteTitle: null,
    auth_code:null,
    unit_a:0,
    building_a:0,
    door_a:0,
    building:'',
    display:'block',
    displays: 'none',
    code: '获取验证码',
    list: [],
    group: [],
    data:false,
    building: [],
    value: [0, 0],
    svn: true,
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options){
    wx.removeStorageSync('building');
    console.log(wx.getStorageSync('community'));
    var that = this;
    wx.request({
      url: getApp().globalData.urlPath + 'Apartment/test',
      data: {
        community: wx.getStorageSync('community'),
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        var group = res.data.key;
        var building = res.data.value[0];
        that.setData({
          list: res.data,
          group: group,
          building: building,
        })
      }
    })
  },
  bindChange: function (e) {
    var that = this;
    const val = e.detail.value;
    var group = that.data.group[val[0]];
    var building = that.data.list.value[val[0]];
    that.setData({
      value: val,
      group: that.data.group,
      building: that.data.list.value[val[0]],
    })
  },
  hiddenFloatView: function () {
    var that = this;
    var val = that.data.value
    var group = that.data.group[val[0]];
    var building = that.data.list.value[val[0]];
    console.log(group);
    console.log(building[val[1]]);
    wx.setStorageSync('group', group);
    wx.setStorageSync('building', building[val[1]]);
    that.unit(wx.getStorageSync('building'));
    this.setData({
      svn: true,
      data:true,
    })
  },
  test: function () {
    this.setData({
      svn: false,
    })
  },
  voteTitle: function (e){
    this.data.voteTitle = e.detail.value;
  },
  auth_code:function(e){
    this.data.auth_code = e.detail.value;
  },
  unit_aChange:function(e){
    this.setData({
      unit_a: e.detail.value,
    });
    this.room(this.data.unit_a);
  },
  door_aChange:function(e){
    this.setData({
      door_a: e.detail.value,
    });
  },
  goGetCode: function () {
    var that = this;
    var time = 60;
    that.setData({
      code: '60秒后重发',
      display: 'none',
      displays:'block',
    })
    var code =  Math.ceil(Math.random() * 9999);
    wx.setStorageSync('code', code);
    wx.request({
      url: getApp().globalData.urlPath + 'community/attesTation',
      data: { 
        code: code, 
        photo: this.data.voteTitle
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res){
        if (res.data.Code === 'isv.BUSINESS_LIMIT_CONTROL'){
          wx.showModal({
            title: '提示',
            content: '一个小时只能请求五次短信',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else {
                console.log('用户点击取消')
              }
            }
          })
        }
      }
    })
    var Interval = setInterval(function(){
      time--;
      if (time > 0) {
        that.setData({
          code: time + '秒后重发',
          disabled: false
        })
      } else {
        clearInterval(Interval);
        that.setData({
          code: '获取验证码',
          display: 'block',
          displays: 'none',
        })
      }
    }, 1000)
  },
  unit:function(data){
    var that = this;
    var building = data;
    wx.request({
      url:getApp().globalData.urlPath+'apartment/unit',
      data: { 
        building:building,
        community:wx.getStorageSync('community'),
        group: wx.getStorageSync('group'),
      },
      header:{
        'content-type':'application/json'
      },
      success: function(res){
        that.setData({
          unit_distance:res.data,
        });
      }
    })
  },
  room:function(data){
    var that = this;
    var unit = this.data.unit_distance[data];
    var building = wx.getStorageSync('building');
    wx.request({
      url: getApp().globalData.urlPath + 'apartment/room',
      data: { 
        building: building,
        unit: unit,
        community: wx.getStorageSync('community'),
        group: wx.getStorageSync('group'),
      },
      header: {
        'content-type': 'application/json'
      },
      success: function (res) {
        that.setData({
          door_distance:res.data,
        });
      }
    })
  },
  search_aSubmit: function (e){
    if (e.detail.value.unit_distance == null) {
      wx.showModal({
        title: '提示',
        content: '单元不能为空',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else {
            console.log('用户点击取消')
          }
        }
      })
      return false;
    }
    if (e.detail.value.door_distance == null){
      wx.showModal({
        title: '提示',
        content: '户号不能为空',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else {
            console.log('用户点击取消')
          }
        }
      })
      return false;
    }
    var regu = /^1[34578]\d{9}$/;
    if (!regu.test(this.data.voteTitle)){
      wx.showModal({
        title: '提示',
        content: '手机认证失败',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else {
            console.log('用户点击取消')
          }
        }
      })
      return false;
    }
    if(this.data.auth_code == null){
      wx.showModal({
        title: '提示',
        content: '验证码不能为空',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else {
            console.log('用户点击取消')
          }
        }
      })
      return false;
    }
    if (this.data.auth_code != wx.getStorageSync('code')){
      wx.showModal({
        title: '提示',
        content: '验证码认证失败',
        success: function (res) {
          if (res.confirm) {
            console.log('用户点击确定')
          } else {
            console.log('用户点击取消')
          }
        }
      })
      return false;
    }
    wx.request({
      url: getApp().globalData.urlPath + 'index/getPerfect',
      method: "POST",
      data: {
          openid: wx.getStorageSync('openid'),
          group: wx.getStorageSync('group'),
          community: wx.getStorageSync('community'),
          building: wx.getStorageSync('building'),
          unit: e.detail.value.unit_distance,
          door: e.detail.value.door_distance,
          phone: this.data.voteTitle,
          code: this.data.auth_code
      },
      header: {
         "Content-Type": "application/x-www-form-urlencoded"
      },
      success: function(res){
        wx.removeStorageSync('community');
        console.log(res.data);
        if(res.data.code == '1003'){
          wx.showModal({
            title: '提示',
            content: '手机号匹配失败',
            success: function (res) {
              if (res.confirm) {
                console.log('用户点击确定')
              } else {
                console.log('用户点击取消')
              }
            }
          })
        }else{
          wx.navigateTo({
            url: '../billing/billing'
          })
        }
      }
    })
  },
  selsectdate: function () {
    this.setData({
      showWin: true  //控制弹窗隐藏显示
    })
  },
  /**
   * 生命周期函数--监听页面初次渲染完成
   */
  onReady: function () {

  },

  /**
   * 生命周期函数--监听页面显示
   */
  onShow: function () {

  },

  /**
   * 生命周期函数--监听页面隐藏
   */
  onHide: function () {

  },

  /**
   * 生命周期函数--监听页面卸载
   */
  onUnload: function () {

  },

  /**
   * 页面相关事件处理函数--监听用户下拉动作
   */
  onPullDownRefresh: function () {

  },

  /**
   * 页面上拉触底事件的处理函数
   */
  onReachBottom: function () {

  },

  /**
   * 用户点击右上角分享
   */
  onShareAppMessage: function () {

  }
})